# -*- coding = utf-8 -*-
# @Time : 2023/5/9 15:48
# @Author :Henry L
# @File : HLJ.py
# @Software : PyCharm
import glob
import os
import sys
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
# 将上级目录添加到模块搜索路径中
# 获取当前文件的绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 获取上级目录的绝对路径
parent_dir = os.path.dirname(current_dir)
# 将上级目录添加到模块搜索路径中
sys.path.append(parent_dir)
# 导入上级目录下的 setting 模块
import setting


class HLJ:
    def __init__(self):
        options = webdriver.ChromeOptions()
        service = Service(r'D:\chromedriver-win64\chromedriver.exe')
        prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'D:\\天眼查自动采集\\'}
        options.add_experimental_option('prefs', prefs)
        self.url = 'http://www.ahyycg.cn/index/notice.html'
        self.driver = webdriver.Chrome(options=options, service=service)
        self.driver.maximize_window()
        self.wait = WebDriverWait(driver=self.driver, timeout=10)
        self.driver.get(self.url)

    def get(self, page):
        time.sleep(3)
        lan = self.driver.find_elements(By.XPATH, "/html/body/div[1]/div[2]/div[3]/div[1]/div[2]/div/div[2]/div")
        for item in lan:
            time.sleep(3)
            if item.find_element(By.XPATH, "div[1]/div[2]").text == '':
                continue
            item.find_element(By.XPATH, "div[1]/div[2]").click()
            handles = self.driver.window_handles
            self.driver.switch_to.window(handles[-1])
            time.sleep(1.5)
            title = self.driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div[3]/div/div[1]/div[1]").text
            box = self.driver.find_element(By.CLASS_NAME, "detail-box")
            date = box.find_element(By.CLASS_NAME, "dbt-des").text.replace('信息来源： 安徽省医药价格和集中采购中心', '').replace(
                "发布时间：", '').split(' ')[0]
            view_time = self.driver.find_element(By.ID, "clicks").text
            content = box.find_element(By.CLASS_NAME, "db-content").get_attribute("outerHTML")
            attachments = box.find_elements(By.TAG_NAME, "a")
            attachments_list = []
            if len(attachments) > 0:
                for a in attachments:
                    if a.text == '' or a.get_attribute("href") is None:
                        continue
                    elif "附件" in a.text or "附表" in a.text:
                        a.click()
                        href = a.get_attribute("href").split('.')[0]
                        # a_name = href.split('/')[-1]
                        # attachments_list.append(a_name)
                        # self.get_last_filename_and_rename(os.getcwd(), a_name)
            time.sleep(1)
            self.driver.close()
            handles = self.driver.window_handles
            self.driver.switch_to.window(handles[-1])
            data_list = [title, date, view_time, content, attachments_list]
            print(data_list)

    def get_last_filename_and_rename(self, sav_folder, new_filename):
        files = glob.glob(sav_folder + '/*')
        max_file = max(files, key=os.path.getctime)
        filename = max_file.split("/")[-1].split(".")[0]
        new_path = max_file.replace(filename, new_filename)
        os.rename(max_file, new_path)
        return new_path

    def insertinto_database(self):
        user = setting.USER
        host = setting.HOST
        password = setting.PASSWORD
        port = setting.PORT
        database_name = setting.DATABASE
        table_name = setting.SOURCE_TABLE


    def close(self):
        time.sleep(60)
        self.driver.quit()



hlj = HLJ()
hlj.get("")
hlj.close()

